package com.lighting.common.core.constants;

import com.lighting.common.core.vo.Context;

import java.util.Optional;

public class CommonContext {


    public static ThreadLocal<Context> threadLocal = new ThreadLocal<>();

    public static Context getOrDefault() {
        return Optional.ofNullable(threadLocal.get()).orElse(new Context());
    }

    public static Long getUserId() {
        return getOrDefault().getUserId();
    }

    public static void setUserId(Long userId) {
        Context context = getOrDefault();
        context.setUserId(userId);
        threadLocal.set(context);
    }

    public static UserTypeEnum getUserType() {
        return getOrDefault().getUserType();
    }

    public static void setUserType(UserTypeEnum userType) {
        Context context = getOrDefault();
        context.setUserType(userType);
        threadLocal.set(context);
    }

    public static void clear() {
        threadLocal.remove();
    }

}
